package cn.lxg.dao.service.impl;

import cn.lxg.dao.entity.SuperLottery;
import cn.lxg.dao.mapper.SuperLotteryMapper;
import cn.lxg.dao.service.ISuperLotteryService;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxg
 * @since 2024-12-02
 */
@Service
public class SuperLotteryServiceImpl extends ServiceImpl<SuperLotteryMapper, SuperLottery> implements ISuperLotteryService {

    @Resource
    private SuperLotteryMapper superLotteryMapper;

    private List<Week> drawDays = new ArrayList<>();

    {
        drawDays.add(Week.MONDAY);
        drawDays.add(Week.WEDNESDAY);
        drawDays.add(Week.SATURDAY);
    }


    @Override
    public List<SuperLottery> getLatelySizeBySameWeek(int size) {
        String latelyDrawingWeek = getLatelyDrawingWeek();
        LambdaQueryWrapper<SuperLottery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SuperLottery::getWeek, latelyDrawingWeek);
        wrapper.last("order by code desc  limit "+ size);
        return superLotteryMapper.selectList(wrapper);
    }

    @Override
    public List<Integer> getAllRedSet(List<SuperLottery> thirtyByWeek) {
        List<Integer> redSet = new ArrayList<>();
        for (SuperLottery lottery :thirtyByWeek) {
            redSet.add(lottery.getRed1());
            redSet.add(lottery.getRed2());
            redSet.add(lottery.getRed3());
            redSet.add(lottery.getRed4());
            redSet.add(lottery.getRed5());
        }
        return redSet;
    }

    @Override
    public List<Integer> getAllBlueSet(List<SuperLottery> thirtyByWeek) {
        List<Integer> buleSet = new ArrayList<>();
        for (SuperLottery lottery :thirtyByWeek) {
            buleSet.add(lottery.getBlue1());
            buleSet.add(lottery.getBlue2());
        }
        return buleSet;
    }

    @Override
    public List<SuperLottery> getLatelySizeBySameDay(int size) {
        String latelyDrawingWeek = getLatelyDrawingDay();
        LambdaQueryWrapper<SuperLottery> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(SuperLottery::getDate, latelyDrawingWeek);
        wrapper.last("order by code desc  limit "+ size);
        return superLotteryMapper.selectList(wrapper);
    }

    @Override
    public List<SuperLottery> getLatelySizeBySamePeriod(int size) {
        LambdaQueryWrapper<SuperLottery> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("order by code desc  limit "+ 1);
        SuperLottery lotteries = superLotteryMapper.selectList(wrapper).get(0);
        Long period = Long.valueOf(lotteries.getCode().toString().substring(4)) ;

        LambdaQueryWrapper<SuperLottery> wrapperPeriod = new LambdaQueryWrapper<>();
        wrapperPeriod.like(SuperLottery::getCode,period);
        wrapperPeriod.last("order by code desc  limit "+ size);
        return superLotteryMapper.selectList(wrapperPeriod);
    }

    @Override
    public List<SuperLottery> getLatelySizeByDay(int size) {
        LambdaQueryWrapper<SuperLottery> wrapper = new LambdaQueryWrapper<>();
        wrapper.last("order by code desc  limit "+ size);
        return superLotteryMapper.selectList(wrapper);
    }

    /**
     * 最近
     *
     * @return
     */
    private String getLatelyDrawingDay() {
        Date drawDay = DateUtil.yesterday();
        while (true) {
            drawDay = DateUtil.offsetDay(drawDay, 1);
            Week week = DateUtil.dayOfWeekEnum(drawDay);
            if (drawDays.contains(week)) {
                return DateUtil.format(drawDay,"MM-dd") ;
            }
        }
    }

    /**
     * 最近
     *
     * @return
     */
    private String getLatelyDrawingWeek() {
        Date drawDay = DateUtil.date();
        int a = 0;
        while (true) {
            drawDay = DateUtil.offsetDay(drawDay, a==0? 0:1);
            a=1;
            Week week = DateUtil.dayOfWeekEnum(drawDay);
            if (drawDays.contains(week)) {
                return week.equals(Week.MONDAY) ? "一" : week.equals(Week.WEDNESDAY) ? "三" : "六";
            }
        }
    }
}
